<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>事件处理</h2>
      <p>count: {{ count }}</p>
      <!-- 内联事件处理器 -->
      <button v-on:click="count++">btn</button>
      <br />
      <!-- 方法事件处理器 -->
      <!-- v-on可以缩写成@ -->
      <!-- 事件后面的括号是可加可不加的 -->
      <button @click="handleClick()">btn2</button>

      <hr />

      <!-- 事件不加括号的情况下都可以自动得到一个事件对象event -->
      <button @click="handleClick2">获取event</button>

      <hr />

      <!-- 如果有括号，还要传event事件对象，需要手动传入$event -->
      <button @click="handleClick3(10, $event)">事件传参10</button>
      <button @click="handleClick3(20, $event)">事件传参20</button>
      <ul>
        <li v-for="item in list" :key="item.id">
          {{ item.name }} - <button @click="remove(item.id)">删除</button>
        </li>
      </ul>
    </div>

    <script src="./lib/vue3.js"></script>
    <script>
      const { createApp } = Vue;

      createApp({
        data() {
          return {
            count: 1,
            list: [
              {
                id: 1,
                name: "zhangsan",
              },
              {
                id: 2,
                name: "lisi",
              },
            ],
          };
        },
        methods: {
          handleClick() {
            this.count++;
          },
          handleClick2(event) {
            console.log(event);
          },
          handleClick3(n, e) {
            console.log(n, e);
          },
          remove(id) {
            console.log(id);
          },
        },
      }).mount("#app");
    </script>
  </body>
</html>
